package com.zhengke.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.pojo.CommonGoodsHeart;
import com.zhengke.pojo.MallSupplier;
import com.zhengke.pojo.Result;
import com.zhengke.pojo.TvPurchase;
import com.zhengke.pojo.TvPurchaseDetails;
import com.zhengke.service.PurchasingService;
import com.zhengke.service.cggl.CGGL020Service;

/**  

* 创建时间：2020年1月6日 下午4:06:22  

* 项目名称：tvmall  

* @author 刘浩  

* @version 1.0     

* 文件名称：PurchasingController.java   

* 类说明：  采购管理控制层

*/
@Controller
@RequestMapping("purchasing")
public class PurchasingController {
	@Autowired
	PurchasingService ps;
	/**
	 * 	到采购管理页面
	 * @return
	 */
	@RequestMapping("goPurchasing.do")
	public String goPurchasing() {
		return "heart/purchasingList";
	}
	/**
	 * 	 查询采购管理数据
	 * @param tp
	 * @return
	 */
	@ResponseBody
	@RequestMapping("selectPurchasing.do")
	public Result selectPurchasing(TvPurchase tp) {
		Result selectTvPurchase = ps.selectTvPurchase(tp);
		return selectTvPurchase;
	}
	/**
	 * 	批量删除 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("deletePurchasing.do")
	public int deletePurchasing(@RequestBody String id) {
		int deleteBaleGoods = ps.deleteTvPurchase(id);
		return deleteBaleGoods;
	}
	/**
	 * 	单个删除 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("deletePurchasingCz.do")
	public int deletePurchasingCz(String id) {
		int deleteBaleGoods = ps.deleteTvPurchase(id);
		return deleteBaleGoods;
	}
	/**
	 * 	确认订购单
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("confirmPurchasing.do")
	public int confirmPurchasing(String purchaseId) {
		int confirmTvPurchase = ps.confirmTvPurchase(purchaseId);
		return confirmTvPurchase;
	}
	/**
	 * 	去采购单详情
	 * @return
	 */
	@RequestMapping("goPurchasingDetailsList.do")
	public String goPurchasingDetailsList(TvPurchaseDetails tpd,ModelMap model) {
		model.addAttribute("purchaseId",tpd.getPurchaseId());
		return "heart/purchasingDetailsList";
	}
	
	/**
	 * 	查询采购单详情
	 * @param tpd
	 * @return
	 */
	@ResponseBody
	@RequestMapping("selectTvPurchaseDetails.do")
	public Result selectTvPurchaseDetails(TvPurchaseDetails tpd) {
		Result rs = ps.selectTvPurchaseDetails(tpd);
		return rs;
	}
	
	@Autowired
	CGGL020Service cs;
	/**
	 * 	去增加订单管理页面
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("goAddTvPurchase.do")
	public String goAddTvPurchase()  {
		return "heart/addTvPurchase";
	}
	
	/**
	 * 	查询供应商
	 * @throws Exception 
	 * 
	 */
	@ResponseBody
	@RequestMapping("selectGYS.do")
	public Result selectGYS(int page,int limit) throws Exception {
		PageHelper.startPage(page,limit,true);
		List<MallSupplier> queryMallSupplier = cs.queryMallSupplier();
		Result rs = new Result();
		PageInfo<MallSupplier> pageInfo = new PageInfo<MallSupplier>(queryMallSupplier);
		rs.setCount(Integer.parseInt(String.valueOf(pageInfo.getTotal())));
		rs.setData(queryMallSupplier);
		rs.setCode(0);
		rs.setMsg("success");
		return rs;
	}
	
	/**
	 * 	去增加订单管理页面2
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("goAddTvPurchase2.do")
	public String goAddTvPurchase2(String brandId,String supplierName,Model md)  {
		md.addAttribute("brandId",brandId);
		md.addAttribute("supplierName",supplierName);
		return "heart/addTvPurchase2";
	}
	
	/**
	 * 根据服务点查询其拥有的商品
	 * @param cg
	 * @return
	 */
	@ResponseBody
	@RequestMapping("selectCommonGoodsHeart.do")
	public Result selectCommonGoodsHeart(CommonGoodsHeart cg) {
		Result selectCommonGoodsHeart = ps.selectCommonGoodsHeart(cg);
		return selectCommonGoodsHeart;
	}
	/**
	 * 	去查看库存页面
	 * @param goodsName
	 * @param md
	 * @return
	 */
	@RequestMapping("goAddTvPurchase3.do")
	public String goAddTvPurchase3(String goodsName,String goodsCommonId,Model md) {
		md.addAttribute("goodsName", goodsName);
		md.addAttribute("goodsCommonId", goodsCommonId);
		return "heart/addTvPurchase3";
	}
	/**
	 * 	增加采购单
	 * @param tp
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addTvPurchase.do")
	public int addTvPurchase(TvPurchase tp) {
		int addTvPurchase = ps.addTvPurchase(tp);
		return addTvPurchase;
	}
	/**
	 * 	增加采购单详情
	 * @param tp
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addTvPurchaseDetails.do")
	public int addTvPurchaseDetails(@ModelAttribute("TvPurchaseDetails")TvPurchaseDetails tpd) {
		int addNum = 0;
		System.out.println("=================="+tpd);
		String str = tpd.getAr();
		String str2 = tpd.getAr2();
		String str3 = tpd.getAr3();
		int length = str.length();
		String substring = str.substring(2, length-2);
		int length2 = str2.length();
		String substring2 = str2.substring(1, length2-1);
		int length3 = str3.length();
		String substring3 = str3.substring(2, length3-2);
		String fh = "\""+","+"\"";
		String[] num = substring.split(fh);
		String[] num2 = substring2.split(",");
		String[] num3 = substring3.split(fh);
		List<String> list = new ArrayList<String>();
		List<String> asList = Arrays.asList(num);
		List<String> asList2 = Arrays.asList(num2);
		List<String> asList3 = Arrays.asList(num3);
		list.addAll(asList);
		list.addAll(asList2);
		list.addAll(asList3);
		for (int j = 0; j < num.length; j++) {
			int parseInt = Integer.parseInt(list.get(j));
			short parseShort = Short.parseShort(list.get(j+num.length));
			int parseInt1 = Integer.parseInt(list.get(j+2*num.length));
			tpd.setGoodId(parseInt);
			tpd.setNumber(parseInt1);
			tpd.setType1(parseShort);
			ps.addTvPurchaseDetails(tpd);
			addNum = ps.addTvPurchaseTemporary(tpd);
		}
		System.out.println(tpd);
		return addNum;
	}
	/**
	 * 	到修改采购单页面
	 * @param tpd
	 * @param model
	 * @return
	 */
	@RequestMapping("goPurchasingDetailsListUpdate.do")
	public String goPurchasingDetailsListUpdate(TvPurchaseDetails tpd,ModelMap model) {
		model.addAttribute("purchaseId",tpd.getPurchaseId());
		return "heart/purchasingDetailsListUpdate";
	}
	/**
	 * 	更改采购单数量
	 * @param tpd
	 * @return
	 */
	@ResponseBody
	@RequestMapping("UpdateTvPurchaseDetails.do")
	public int UpdateTvPurchaseDetails(TvPurchaseDetails tpd) {
		int updateTvPurchaseDetails = ps.UpdateTvPurchaseDetails(tpd);
		return updateTvPurchaseDetails;
	}
}
